(function(win){
    "use strict";
    if(window.FastClick)FastClick.attach(document.body);
    //解决:active问题
    document.addEventListener("touchstart",function(){});
    
    
    $('#return').click(function(){
    	window.history.go(-1);
    });

    //显示菜单
    var navDom = $("#nav");
    $("#navbtn").click(function(){
    	if(navDom.length <= 0)return;
    	navDom.finish().fadeIn();
        $("#zhezhao").show();
    });
    $("#zhezhao").on("touchmove",function(e){
    	e.preventDefault();
    })
    $("#zhezhao").on("touchstart",function(){
    	navDom.finish().fadeOut();
        $(this).hide();
    });
    
    //导航提醒
	navDom.children("ul").children("li").each(function(){
		var that = $(this),
			state = that.data("state");
		if(state == "3"){//有反馈
			that.children("a").prepend("<i class='iconfont'>&#xe618;</i>").show();
		}else if(state == "5"){//通过
			that.children("a").prepend("<i class='iconfont'>&#xe641;</i>").show();
		}
	})
    
    /**
     * 空判断
     * @param val
     * @returns
     */
    function isEmpty(val){
        if(val == null || val == undefined ||  val === "" || val.toString().length == 0){
            return true;
        }else{
            return false;
        }
    };
    
    /**
     * json对象格式化--》a=1&b=1  用于axios post数据发送
     * @param json
     * @returns
     */
    function qsStringify(json){
    	if(isEmptyObj(json))return ""
		var stringJson = ""
		for(var key in json){
			if(!isEmpty(stringJson)){
				stringJson += "&" + key +"="+ json[key]
			}else{
				stringJson += key +"="+ json[key]
			}
		}
		return stringJson
	}
    
    /**
     * 判断空对象
     * @param obj
     * @returns
     */
    function isEmptyObj(obj){
		for(var key in obj){
			return false;
		}
		return true;
	}
    
    /**
     * 清除所有空格
     * @param str
     * @returns
     */
    function trimAll(str){
        var reg = new RegExp(" ","g");
        return str.replace(reg,"");
    };

    /**
     * 显示弹窗
     * @param text
     * @returns
     */
    function showTip(text,opt){
        if($("#tip").length == 0){
            $("body").append("<div id='tip'></div>");
        };
        var tipDom = $("#tip");
        if(!isEmpty(text)){
            text = trimAll(text);
            if(opt){
            	var timer = null,
            		seconds = 3;
            	tipDom.stop(true,false).html(text + "<br />3秒后跳转").css({
            		"margin-top":-tipDom.outerHeight() / 2,
            		"margin-left":-tipDom.outerWidth() / 2
            	}).fadeIn(300)
            	
            	timer = setInterval(function(){
            		seconds--
            		tipDom.html(text + "<br />" + seconds + "秒后跳转")
            		if(seconds <= 0){
        		    	window.location.href = opt.url
            			clearInterval(timer)
            		}
            	},1000)
            }else{
            	tipDom.stop(true,false).text(text).css({
            		"margin-top":-tipDom.outerHeight() / 2,
            		"margin-left":-tipDom.outerWidth() / 2
            	}).fadeIn(300).delay(1500).fadeOut(300);
            }
        }
    };
    
    /**
     * 图片等比例缩放
     * @param imgW
     * @param imgH
     * @param maxWidth
     * @param maxHeight
     * @returns json
     */
    function imgFormat(imgW,imgH,maxWidth,maxHeight){
        var _width,_height;
        if(maxWidth / maxHeight <= imgW / imgH){
            _width = maxWidth;  
            _height = maxWidth * (imgH / imgW); 
        }else{
            _width = maxHeight * (imgW / imgH);  
            _height = maxHeight;  
        }
        if(imgW < maxWidth && imgH < maxHeight){
            _width = imgW;
            _height = imgH;
        }
        return {
            "imgWidth":_width,
            "imgHeight":_height
        }
    };
    
    /**
     * 数组去重
     * @param oldarr
     * @returns newArr
     */
    function unique(oldarr){
        var newArr = [];
        oldarr.forEach(function(obj){
            if(newArr.indexOf(obj) === -1){
                newArr.push(obj);
            }
        });
        return newArr;
    };
    
    /**
     * 匹配图片格式
     * @param type
     * @returns mark
     */
    function isImage(type){
        var mark = false;
        type = type.toLowerCase();
        switch(type){
            case ".jpg":
            case ".jpeg":
            case ".png":
            case ".gif":
                mark = true;
            break;
            default:
                mark = false;
            break;
        }
        return mark;
    };
    
    /**
     * 字节转换
     * @param btyes
     * @returns
     */
    function bytesTosize(btyes){
         if(btyes >= 1024 && btyes < 1048576){
             return (btyes / 1024).toFixed(2) + "KB";
         }else{
             return (btyes / 1024 / 1024).toFixed(2) + "MB";
         }
     };
     
     /**
      * 窗口滚动动画
      * @param A起始位置
      * @param B目标位置
      * @param rate缓动速率
      * @param callback
      * @returns
      */
     function scrollAnimate(A,B,rate,callback){
		if(A == B || typeof A != 'number') {
	        return;    
	    }
		rate = rate || 2;
		B = B || 0;
		var step = function(){
			A = A + (B - A) / rate;
			
			if(Math.abs(A - B) < 1){
				callback(B,true);
				return;
			}
			callback(A,false);
			requestAnimationFrame(step);
		}
		step();
	}
    
	/**
	 * 获取图片真实宽度真实高度
	 * @param src
	 * @returns json
	 */
    function getImgWH(src){
    	if(isEmpty(src))return;
    	var image = new Image(),
    		json = {};
    	image.src = src;
    	image.onload = function(){
    		json.w = this.width;
    		json.h = this.height;
    	}
    	return json;
    }
    
    /**
     * 判断是否是安卓设备
     * @returns
     */
    function isAndroid(){
		var u = navigator.userAgent
		if(u.indexOf('Android') > -1 || u.indexOf('Adr') > -1){
			return true
		}else{
			return false
		}
	}
    
    /**
     * 等待动画
     */
    function window_loading(close){
    	var dom = $("#window_loading")
    	if(close){
    		$("#window_loading").stop(true).fadeOut(300)
    		$("#window_loading").children(".loading").removeClass("animate")
    	}else{
    		if(dom.length === 0){
    			var size = $(document).width() * .4,
    			html = "<div id='window_loading' style='width: "+ size +"px;height: "+ size +"px;margin-top: "+ -(size / 2) +"px;margin-left: "+ -(size / 2) +"px;'>"+
		    		   "	<div class='loading'></div>"+
		    		   "</div>";
    			$("body").append(html)
    		}
    		
    		$("#window_loading").children(".loading").addClass("animate")
    		$("#window_loading").stop(true).fadeIn(300)
    	}
    }
    
    /**
	 * 图片加载函数
	 * callback 1成功 0失败
	 */
	function imageLoad (src,callback) {
		var image = new Image()
		image.src = src
		if (image.complete) {
			callback.call(image, 1)
			return
		}
		
		image.onload = function(){
			callback.call(this, 1)
		}
		
		image.onerror = function(){
			callback.call(this, 0)
		}
	}
	
	/**
	 * sessionStorage存储
	 * key存储名字
	 * json对象
	 */
	function setSessionStorage (key, json) {
		window.sessionStorage.setItem(key, JSON.stringify(json))
	}
	
	/**
	 * sessionStorage获取
	 * key存储名字
	 * 
	 */
	function getSessionStorage (key) {
		return JSON.parse(window.sessionStorage.getItem(key))
	}
	
	/**
	 * sessionStorage删除
	 * key存储名字
	 * 
	 */
	function removeSessionStorage (key) {
		window.sessionStorage.removeItem(key)
	}
	
	/**
	 * 产品培训注释模板
	 */
	function showTraninNotes (txt) {
		var tranin = $("#training_notes")
		if (_x.isEmpty(tranin[0])) {
			var html = "<div id='training_notes'>"+
						"	<div class='content flexbox'>"+
						"		<img src='/assets/img/zhushitubiao2@2x.png' />"+
						"		<p>"+ txt +"</p>"+
						"	</div>"+
						"</div>"
			$("body").append(html)
			$("#training_notes").fadeIn(200)
			$("#training_notes")[0].addEventListener("touchmove", function (e) {
				e.preventDefault()
			})
			$("#training_notes").click(function(){
				$(this).fadeOut(200)
			})
		} else {
			tranin.fadeIn(200).find("p").text(txt)
		}
	}
    
    window._x = {
        "isEmpty":isEmpty,
        "trimAll":trimAll,
        "imgFormat":imgFormat,
        "showTip":showTip,
        "unique":unique,
        "isImage":isImage,
        "bytesTosize":bytesTosize,
        "isEmptyObj":isEmptyObj,
        "scrollAnimate":scrollAnimate,
        "getImgWH":getImgWH,
        "qsStringify":qsStringify,
        "isAndroid":isAndroid,
        "window_loading": window_loading,
        "imageLoad": imageLoad,
        "setSessionStorage": setSessionStorage,
        "getSessionStorage": getSessionStorage,
        "removeSessionStorage": removeSessionStorage,
        "showTraninNotes": showTraninNotes
    }
})(window)